<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div>
        求1+2!+3!+...+20!的值

        2! 表示 2 的阶乘 2 * 1
        3! 表示 3 的阶乘 3 * 2 * 1
    </div>
    <div>
        封装一个函数，这个函数的作用，用户输入一个值，求这个数以内所有的数的阶乘的和
    </div>
    <input type="text" id="num">
    <button id="btn">计算</button>
    <div id="res">

    </div>
    <script>
        // var res = 0 ;
        // var num = 1 ;
        // for( var a = 1 ; a <= 20 ; a ++ ){
        //     for ( var b = 1 ; b <= a ; b ++ ){
        //         num = num * b ;
        //     }
        //     res = res + num ;
        // }
        // console.log(res);
        function factorial (a) {
            var res = 1 ; 
            for ( var i = 1 ; i <= a ; i ++ ) {
                res = res * i ;
            }
            return res ; 
        }
        function plus (a) {
            var res = 0 ;
            for ( var i = 1 ; i <= a ; i ++ ) {
                res = res + factorial(i) ;
            }
            return res ; 
        }
        var num = document.getElementById("num");
        var btn = document.getElementById("btn");
        btn.onclick = function () {
            var a = parseInt(num.value);
            console.log(plus(a));
            document.getElementById("res").innerHTML = plus(a);
        }
    </script>
</body>
</html>